package database;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import REUtils.showMsg;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import globalpara.* ;

public class BigTableUtils {	
	  public static Key getKeyForName(String stkNUM) {
	    return KeyFactory.createKey(BigTable.class.getSimpleName(),stkNUM);
	  }
	  
//write data to Bigtable	  
	  public static boolean addRowTo(String stkNUM,
			                      List<String> ListStrs ){  
	    
		if(ListStrs.size() > Gpara.MaxDBListSize ){
			showMsg.DEBUG("BT.addRowto() 's Listsize is more than  "+Gpara.MaxDBListSize);
			return false ;
		}  
		  
	    PersistenceManager pm = connection.get().getPersistenceManager();	    
	    String meKey = KeyFactory.keyToString(getKeyForName(stkNUM));
	    
	    
	    BigTable other = null;
	    try {
	      pm.currentTransaction().begin();
	      try {
	        //other = pm.getObjectById(BigTable.class, otherKey);
	    	other = pm.getObjectById(BigTable.class, meKey);
	    	List<String> replacementFriends = new ArrayList<String>(other.getBigTableKeys());
	        replacementFriends.add(meKey);
	        other.setBigTableKeys(replacementFriends);
	      } catch (JDOObjectNotFoundException e) {
	        
	    	other = new BigTable(stkNUM, ListStrs);//sKeyValue); ///*sTAG,sSeason,sValue*/
                                       
	        List<String> replacementFriends = new ArrayList<String>(other.getBigTableKeys());
	        replacementFriends.add(meKey);
	        other.setBigTableKeys(replacementFriends);
	        pm.makePersistent(other);
	      }
	      pm.currentTransaction().commit();
	    } finally {
	      if (pm.currentTransaction().isActive()) {
	        pm.currentTransaction().rollback();
	      }
	    }
	    pm.close();
	    return true ;
	  }
	  
	  public static List<BigTable> getBTList(String stkNUM) {
		    PersistenceManager pm = connection.get().getPersistenceManager();
		    
		    Query query = pm.newQuery(BigTable.class);
		    String myKey = KeyFactory.keyToString(getKeyForName(stkNUM));
		    query.declareParameters("String myKey");
		    query.setFilter("BigTables == myKey");
		    query.setRange(0,1);

		    List<BigTable> BT1= (List<BigTable>) query.execute(myKey);
		    
		    return BT1;
		  }
//Get BigTables from Database	  
	  
	  public static List<BigTable> getBigtablesOf(String stkNUM/*,String sTAG,String sSeason*/) {
	    PersistenceManager pm = connection.get().getPersistenceManager();
	    
	    Query query = pm.newQuery(BigTable.class);
	    String myKey = KeyFactory.keyToString(getKeyForName(stkNUM));
	    query.declareParameters("String myKey");
	    query.setFilter("BigTables == myKey");
	    //query.setOrdering("lastName ASC, firstName ASC");
	    List<BigTable> BigTables= (List<BigTable>) query.execute(myKey);
	    
	    return BigTables;
	  }
	  
	}

/*	  
public static List<BigTable> getBigtableValue(String stkNUM,String sTAG,String sSeason) {
	    PersistenceManager pm = connection.get().getPersistenceManager();
	    
	    Query query = pm.newQuery(BigTable.class);
	    String myKey = KeyFactory.keyToString(getKeyForName(stkNUM,sTAG,sSeason));
	    query.declareParameters("String myKey");
	    query.setFilter("BigTable == myKey");
	    //query.setOrdering("lastName ASC, firstName ASC");
	    //List<BigTable> BigTables= (List<BigTable>) query.execute(myKey);
	    List<BigTable> BTs = (List<BigTable>) query.execute(myKey);
	    
	    return BTs;
	  }
 public static void insert(String stkNUM,String sTAG,String sSeason,String sValue) {
	    BigTable entry = new BigTable(stkNUM, sTAG, sSeason, sValue);
	    PersistenceManager pm = connection.get().getPersistenceManager();
	    pm.makePersistent(entry);
	  }
*/	  
 
/*		  
	  public static List<BigTable> getEntries() {
	    PersistenceManager pm = connection.get().getPersistenceManager();
	    Query query = pm.newQuery(BigTable.class);
	    query.setOrdering("when DESC");
	    List<BigTable> entries = (List<BigTable>) query.execute();
	    return entries;
	  }
*/

/*	    
pm = connection.get().getPersistenceManager();

// Then add a reference to the friend to my list.

BigTable me = null;
try {
  pm.currentTransaction().begin();
  try {
    me = pm.getObjectById(BigTable.class, meKey);
    List<String> replacementFriends = new ArrayList<String>(
    me.getBigTableKeys());
    //replacementFriends.add(otherKey);
    replacementFriends.add(meKey);
    me.setBigTableKeys(replacementFriends);
  } catch (JDOObjectNotFoundException e) {
    me = new BigTable(stkNUM,sTAG,sSeason,sValue);

    List<String> replacementFriends = new ArrayList<String>(
    me.getBigTableKeys());
    replacementFriends.add(otherKey);
    //replacementFriends.add(meKey);
    me.setBigTableKeys(replacementFriends);
    pm.makePersistent(me);
  }
  pm.currentTransaction().commit();
} finally {
  if (pm.currentTransaction().isActive()) {
    pm.currentTransaction().rollback();
  }
}
*/